#!/bin/sh
# Copyright (c) 2011, bkil.hu
# This program is free software and can be distributed under the terms of
# the GNU General Public License v2,
# see COPYING for detailed licensing terms.

# A simple script for watching TV via MPlayer.
# created on 2011-03-21

main(){
 lq
}

lq() {
 mpl width=720:height=576:fps=25 -sws 4 \
 -vf field=0,scale=360:-1,crop=352:264 #,denoise3d=5:1:7:1

}

sq() {
 mpl width=720:height=576:fps=25
}

hq() {
 mpl width=720:height=576:fps=25 -vf crop=704:528,pp=lb,denoise3d
}

mpl() {
 TVOPT="$1"
 shift 1
 CHANS=`get_chan_string`
 mplayer -vo xv -quiet -fs \
$@ \
 -af volnorm=1:0.5 \
 -tv \
automute=0:driver=v4l2:device=/dev/video0\
:input=0:norm=pal-bg:outfmt=yv12\
:${TVOPT}\
:channels=$CHANS \
tv://4 #15
}
#uyvy

get_chan_string() {
 get_zip_fq_name |
 sed "s/[ :,_-]/_/g" |
 zip_with "-" |
 intersperse ","
}

get_zip_fq_name() {
 grep "<station [^>]*\<name=" ~/.tvtime/stationlist.xml |
 grep " active=\"1\"" |
 sed "s/^.*<station [^>]*\<name=\"\([^\"]*\)\"[^>]*\<channel=\"\([^\".]*\)\.\([^\".]*\)MHz\".*$/\2\30\n\1/" |
 unescape
}

unescape() {
 sed "
  s/&#xED\;/í/g
  s/&#xF3\;/ó/g
 "
}

zip_with() {
 SEP="$1"
 sed "
  N
  s/\n/$SEP/"
}

intersperse() {
 SEP="$1"
 sed "
  :a
  N
  s/\n/$SEP/
  t a
 "
}

main "$@"
exit

# xvinfo: yuy2, yv12, i420, uyvy, xvmc
# immediatemode=0:alsa:adevice=hw.1,0:amode=0:forcechan=2
#-vf field=0,scale=384:-1,crop=352:264,denoise3d=5:1:7:1
# gain=50
# tdevice=/dev/vbi0:tlang=2
